package voip;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import media.textToSpeech;

/*===============================================================
 [-c config_file_name] is to override the default filename and path. 
 The file name can include a relative or absolute path.

 [-user sip_uri] indicates what address the SIP server should listen to. 
 Example robot@127.0.0.1:5064. 
 The default name should be sipspeaker@DEFAULT_INTERFACE:5060. 
 The port should be UDP 5060.
 sipspeaker@127.0.0.1:5060
 [-http http_bind_address] tells the web server what address and 
 port to listen to. Example: 127.0.0.1:8080, 
 or only port 8080, 
 or only address/name: myserver.mydomain.se. 
 Default should be port 80 and default interface.
 ====================================================================*/

public class SIPProperty {
	private final String DEFAULT_FILE = "sipspeaker.cfg";
	// default configuration file
	public static String configFile;

	// Default wave files
	public static String default_message;
	public static String message_content;

	// Default TTL
	public static int message_flag;
	public static String default_message_wav;
	public static String default_message_text;

	// Default Date
	public static String default_message_received;

	// Default SIP Information
	public static String default_sip_interface;
	public static String default_sip_port;
	public static String default_sip_user;
	
	// default_{sip_user + interface + port} = default_sip_uri
	public static String default_sip_uri;
	
	// Default HTTP Information
	public static String default_http_interface;
	public static String default_http_port;
	public static String default_http_bind_address;
	
	// User Command line Input
	public static String sip_uri;
	public static String http_bind_address;

	public static boolean boolCommandInput = false;
	
	public static boolean validUserFlag = false;
	public static boolean inviteReceived = false;
	
	textToSpeech tts;
	
	SIPProperty() 
	{
		setDefaultValue();

	}
	
	public void setDefaultValue()
	{
		
		default_message = "default.wav";
		message_content = "This is the default message generated by the system. You currently have no stored message.";
		tts = new textToSpeech(default_message, message_content);
		tts.record();
		
		default_message_wav = "currentmessage.wav";
		default_message_text = "Welcome to the SIP Speaker this is my own answering machine.";
		tts = new textToSpeech(default_message_wav, default_message_text);
		tts.record();
		message_flag = 1;
		
		default_message_received = "43534522135";

		default_sip_interface = "127.0.0.1";
		default_sip_port = "5060";
		default_sip_user = "sipspeaker";
		
		default_sip_uri = default_sip_user+"@"+default_sip_interface+":"+default_sip_port;;
		
		default_http_interface = "127.0.0.1";
		default_http_port = "80";
		default_http_bind_address = default_http_interface + ":" + default_http_port;
		
		// Command line input variables
		sip_uri = "";
		http_bind_address = "";
		
		boolCommandInput = false;		
		
	}
	public void clearAllProperties()
	{
		configFile = "";

		default_message = "";

		default_message_wav = "";
		default_message_text = "";

		default_message_received = "";

		default_sip_interface = "";
		default_sip_port = "";
		default_sip_user = "";
		
		default_sip_uri = "";
		
		default_http_interface = "";
		default_http_port = "";
		default_http_bind_address = "";
		
		// Command line input variables
		sip_uri = "";
		http_bind_address = "";
		
		boolCommandInput = false;		
	}
	
	public SIPProperty(String filename, String sip_uri_name, String http_address) 
	{
		//clearAllProperties();
		setDefaultValue();
		
		configFile = filename;
		sip_uri = sip_uri_name;
		http_bind_address = http_address;
		
		if((!configFile.isEmpty()) || (!sip_uri.isEmpty()) || (!http_bind_address.isEmpty()))
		{
			System.out.println("Command Line Input OK.");
			boolCommandInput = true;
		}
	}

	public void run(String filename) 
	{
		configFile = filename;
		
		readDefaultConfigFile(filename);
		
		checkCommandLineValue(filename);		
	}
	/*=====================================================================
	 * Read a default configuration file and 
	 * set default values to local class member variables
	 * ===================================================================== */
	public void readDefaultConfigFile(String filename) 
	{
		
		Properties prop = new Properties();
		File infile = new File(filename);

		String dmessage = "";
		String dmessage_wav = "";
		String dmessage_text = "";
		String dmessage_received = "";
		String dsip_interface = "";
		String dsip_port = "";
		String dsip_user = "";
		String dhttp_interface = "";
		String dhttp_port = "";

		try 
		{
			if (!infile.exists()) 
			{
				// If a input file is not exist, then use the default file.
				// (sipspeaker.cfg)
				System.out.println("====================================================================================");
				System.out.println("Default configuration file: \"" + DEFAULT_FILE + "\" will be used for configuration.");
				System.out.println("====================================================================================");
				filename = DEFAULT_FILE;
			}

			FileInputStream input = new FileInputStream(filename);

			prop.load(input);

			// For debugging
			//System.out.println(prop);
			
			dmessage = prop.getProperty("default_message");
			if(!dmessage.isEmpty())
				default_message	 = dmessage;
			
			dmessage_wav 	= prop.getProperty("message_wav");
			if(!dmessage_wav.isEmpty())
				default_message_wav = dmessage_wav;

			dmessage_text = prop.getProperty("message_text");
			if(!dmessage_text.isEmpty())
				default_message_text 	= dmessage_text;
			
			dmessage_received = prop.getProperty("message_received");
			if(!dmessage_received.isEmpty())
				default_message_received = dmessage_received; 
			
			
			// ex) sip_uri = sip_user+"@"+sip_interface+sip_port
			dsip_interface = prop.getProperty("sip_interface");
			if(!dsip_interface.isEmpty())
				default_sip_interface = dsip_interface;
			
			dsip_port = prop.getProperty("sip_port");
			if(!dsip_port.isEmpty())
				default_sip_port = dsip_port;
			
			dsip_user = prop.getProperty("sip_user");
			if(!dsip_user.isEmpty())
				default_sip_user = dsip_user; 
			
			dhttp_interface = prop.getProperty("http_interface");
			if(!dhttp_interface.isEmpty())
				default_http_interface = dhttp_interface;
			
			dhttp_port = prop.getProperty("http_port");
			if(!dhttp_port.isEmpty())
				default_http_port = dhttp_port; 		
			
			input.close();
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
			System.out.println("Cannot find the file.");
			System.exit(0);
		}
	}

	/*=====================================================================
	 * Read a default configuration file and set default values to local class
	 * member variables
	 * ex) java SIPSpeaker [-c config_file_name] [-user sip_uri] [-http http_bind_address]
	 * =====================================================================*/
	public void checkCommandLineValue(String filename) 
	{

		// If there is no command line input for sip_uri, then
		if (sip_uri.isEmpty()) 
		{
			// Use default values from the sipspeaker.cfg file
			default_sip_uri = default_sip_user+"@"+default_sip_interface+":"+default_sip_port;
			System.out.println("default_sip_port: " + default_sip_port);
			System.out.println("default_sip_uri: " + default_sip_uri);
		} 
		else 
		{
			if(sip_uri.contains("@") == false)
			{
				System.out.println();
				System.out.println("[Error] Invalid SIP URI : No Interface");
				System.out.println();
				System.exit(0);
			}
			if(sip_uri.contains(":") == false)
			{
				System.out.println();
				System.out.println("[Error] Invalid SIP URI: No Port Number");
				System.out.println();
				System.exit(0);
			}
			
			default_sip_uri = sip_uri;
		}

		if (http_bind_address.isEmpty()) 
		{
			default_http_bind_address = default_http_interface + ":" + default_http_port; 
		}
		else
		{
			if(http_bind_address.indexOf('.') == -1)
			{
				// No exist dot(.), it can be a port number
				default_http_port = http_bind_address;
				default_http_bind_address = default_http_interface + ":" + default_http_port;
			}
			else if(http_bind_address.indexOf(':') == -1)
			{
				// If a http address or a domain name exists except port number,
				// then add default_http_port
				default_http_bind_address = http_bind_address + ":" + default_http_port;				
			}
			else
				default_http_bind_address = http_bind_address;
		}

		System.out.println("sip_uri: " + default_sip_uri);
		System.out.println("http_bind_address: " + default_http_bind_address);

	}

	/*
	 * =====================================================================
	 * Return default-config values related to an input key
	 * =====================================================================
	 */
	public String getDefaultConfigValue(String key) 
	{
		String stringValue = "";

		if (key == "default_message") 
		{
			stringValue = default_message;
		}
		else if (key == "message_wav") 
		{
			stringValue = default_message_wav;
		}
		else if (key == "message_text") 
		{
			stringValue = default_message_text;
		}
		else if (key == "sip_interface") 
		{
			stringValue = default_sip_interface;
		}
		else if (key == "sip_user") 
		{
			stringValue = default_sip_user;
		}
		else if (key == "sip_port") 
		{
			stringValue = default_sip_port;
			System.out.println("default_sip_port: " + stringValue);
		}
		else if(key == "default_sip_uri")
		{
			stringValue = default_sip_uri;
		}
		else if (key == "http_interface") 
		{
			stringValue = default_http_interface;
		}
		else if (key == "http_port") 
		{
			stringValue = default_http_port;
		}
		else if(key == "default_http_bind_address")
		{
			stringValue = default_http_bind_address;
		}
		else
		{
			stringValue = "Invalid parameter";
		}
		
		return stringValue;
	}

	public void printDefaultProperty()
	{
		System.out.println("[ Default SIP Property ]");
		System.out.println("default_message: " + default_message);
		System.out.println("default_message: " + default_message_wav);
		System.out.println("default_message: " + default_message_text);
		System.out.println("default_message: " + default_sip_interface);
		System.out.println("default_message: " + default_sip_user);
		System.out.println("default_message: " + default_sip_port);
		System.out.println("default_message: " + default_sip_uri);
		System.out.println("default_message: " + default_http_interface);
		System.out.println("default_message: " + default_http_port);
		System.out.println("default_message: " + default_http_bind_address);
		System.out.println("=================================================");
	}
	
	public boolean getCommandLineStatus()
	{
		return boolCommandInput;
	}
}
